/**
 * @author   zhangyp
 * @created  2013-8-12 下午3:15:26
 * @email    zviolet@163.com
 * TODO      初始化APP配置
 */
package com.bible.fire.local;

import java.io.InputStream;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import android.content.Context;
import android.content.res.AssetManager;
import android.util.Log;

import com.bible.fire.bean.system.AppConfig;

public class ConfigManager {
	
	private static String TAG = ConfigManager.class.getSimpleName();
	
	private static AppConfig appConfig;
	
	public static void initAppConfig( Context context ){
		AssetManager am = context.getAssets();
		try {
			InputStream is = am.open("app.xml");
			byte[] buffer = new byte[512];			
			int readnum = 0;
			StringBuffer sb = new StringBuffer();
			while( (readnum = is.read(buffer)) > 0 ){
				sb.append( new String( buffer, 0, readnum ) );
			}
			is.close();
			Document doc = DocumentHelper.parseText( new String(sb) );
			Element root = doc.getRootElement();
			Element server = root.element("server");
			appConfig = new AppConfig(server.elementTextTrim("ip"), Integer.parseInt(server.elementTextTrim("port")),
					                  server.elementTextTrim("basePath"), server.elementTextTrim("versionPath"));			
		}catch(Exception e){
			Log.e(TAG, "init app config", e);
		}		
	}
	
	public static String getLastestVersionUrl(){
		if( appConfig != null ){
			return appConfig.getVersionUrl();
		}
		return "";
	}
	
	public static String getServerUrl(){
		if( appConfig != null ){
			return appConfig.getServerUrl();
		}
		return "";
	}

}
